{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22dbf02f",
   "metadata": {},
   "source": [
    "# 列表的高级方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dfffea5",
   "metadata": {},
   "source": [
    "## 1.copy() 赋值（=）不等于 copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5c428248",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b3c7397",
   "metadata": {},
   "source": [
    "### 实验1——赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "162fcaaf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "# 结论：无法实现真正的复制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b99b85b",
   "metadata": {},
   "source": [
    "### 实验2 copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "967c4e0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "730ad09d",
   "metadata": {},
   "source": [
    "### 实验3——切片实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "677a5e8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "757c835e",
   "metadata": {},
   "source": [
    "## 2.枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27105915",
   "metadata": {},
   "source": [
    "### 枚举：因为list其实不仅有values，还有index索引，但for循环主要循环遍历其值，不遍历索引，因此有了枚举，同时遍历两者"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d46e0944",
   "metadata": {},
   "source": [
    "##### 如何取出所有位置10001的index?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "63de5414",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "0\n",
      "0 1001\n",
      "3 1001\n",
      "7 1001\n",
      "想要删除pop()的值： [0, 3, 7]\n"
     ]
    }
   ],
   "source": [
    "for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 1001:\n",
    "        # 判断如果元素为10001的时候,怎么取出索引值？\n",
    "        print(stu_id.index(i))\n",
    "        \n",
    "\n",
    "## enumerate()\n",
    "pop_values_list = []   #append方法常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    if item == 1001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"想要删除pop()的值：\",pop_values_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed325962",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
